package org.cn.zhaozhx.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * 读文件
 * 
 * @author flying
 * 
 */
public class ReadFile {
    /**
     * 获取文件的内容
     * 
     * @param path
     *            模板文件的路径
     * @param charSet
     *            文件的字符集
     * @return 文件内容
     */
    public static String getFileContent(String path, String charSet) {
        File filekk = new File(path);
        return OpenReadFile(filekk, charSet).toString();
    }
    
    /**
     * 获取文件的内容
     * 
     * @param path
     *            模板文件的路径
     * @return 文件内容
     */
    public static String getFileContent(String path) {
        File filekk = new File(path);
        return OpenReadFile(filekk, "").toString();
    }
    
    public static String getFileContent(File file, String charSet) {
        return OpenReadFile(file, charSet).toString();
    }
    
    /**
     * 文件内容
     * 
     * @param file
     *            文件
     * @param strCharset
     *            文件字符集
     * @return
     */
    private static StringBuffer OpenReadFile(File file, String strCharset) {
        
        StringBuffer textCon = new StringBuffer();
        // String strCharset = "";
        try {
            InputStreamReader reader = null;
            if (strCharset.trim().length() == 0) {
                reader = new InputStreamReader(new FileInputStream(file));
            } else {
                reader = new InputStreamReader(new FileInputStream(file), Charset.forName(strCharset));
            }
            BufferedReader bufferedReader = new BufferedReader(reader);
            String line = bufferedReader.readLine();
            while (line != null) {
                try {
                    String nextLine = bufferedReader.readLine();
                    if (nextLine != null) {
                        line += "\n";
                    }
                    textCon.append(line);
                    
                    line = nextLine;
                } catch (Exception blExc) {
                    blExc.printStackTrace();
                }
            }
            reader.close();
            bufferedReader.close();
        } catch (IOException exc) {
            exc.printStackTrace();
            
        }
        return textCon;
        
    }
    
    public static void main(String[] args) {
        System.out.println(ReadFile.getFileContent(
                "/home/flying/视频/t/25.数据存储(4)(1)/src/19.File/src/com/szy/file/MainActivity.java", "gbk"));
    }
}
